package pers.hl.module_main.mine

import android.os.Bundle
import android.widget.TextView
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.StringUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import pers.hl.lib_global.common.GlideUtils
import pers.hl.lib_global.model.bean.User
import pers.hl.module_main.BaseTitleRvFragment
import pers.hl.module_main.R
import pers.hl.module_main.common.BaseLibQuickAdapter
import pers.hl.module_main.common.PageHelper
import pers.hl.module_main.common.SwipeMenuHelper

class UserListFragment: BaseTitleRvFragment<User>() {

    private var mType = 0

    companion object {

        const val KEY_PAGE_TYPE = "key_page_type"

        const val TYPE_FOLLOW = 1
        const val TYPE_FANS = 2

        @JvmStatic
        fun newInstance(type: Int): UserListFragment {
            val bundle = Bundle()
            bundle.putInt(KEY_PAGE_TYPE, type)
            val fragment = UserListFragment()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initParams() {
        mType = arguments?.getInt(KEY_PAGE_TYPE) ?: 0
    }

    override fun getTitle(): String {
        return when (mType) {
            TYPE_FOLLOW -> "我的关注"
            TYPE_FANS -> "我的粉丝"
            else -> "未知"
        }
    }

    override fun beforeSetAdapter() {
        when (mType) {
            TYPE_FOLLOW -> {
                val cancelFollowCreator = SwipeMenuHelper.createRightItem(mContext, "取关")
                mBinding.recyclerView.setSwipeMenuCreator(cancelFollowCreator)
            }
            TYPE_FANS -> {

            }
        }
    }

    override fun createAdapter(): BaseLibQuickAdapter<User, BaseViewHolder> {

        return object : BaseLibQuickAdapter<User, BaseViewHolder>(R.layout.item_rv_user) {

            override fun convert(holder: BaseViewHolder, item: User) {
                // SwipeRecyclerView与BRVAH冲突，此处拿到的itemView是SwipeMenuLayout，无法使用ViewBinding

                GlideUtils.loadUserAvatar(mContext, holder.getView(R.id.ivAvatar), item.userId, R.drawable.ic_personal_center)
                holder.getView<TextView>(R.id.tvSignature).text = "读万卷书，行万里路"
                holder.getView<TextView>(R.id.tvUserName).text = if (StringUtils.isEmpty(item.userName)) "匿名用户" else item.userName
                holder.itemView.setOnClickListener {
                    PageHelper.startUserHomePage(mContext, item.userId)
                }
            }
        }
    }

    override fun initData(savedInstanceState: Bundle?) {
        when (mType) {
            TYPE_FOLLOW -> {
                mViewModel.fetchMyFollows().observe(this, Observer {
                    fillData(it.toMutableList())
                })
            }
            TYPE_FANS -> {
                mViewModel.fetchMyFans().observe(this, Observer {
                    fillData(it.toMutableList())
                })
            }
        }
    }

    private fun fillData(list: MutableList<User>) {
        if (list.isNullOrEmpty()) {
            mAdapter.setEmptyView()
        } else {
            mAdapter.setList(list)
        }
    }

}